﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Media;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor.Utils
{
    public partial class RevealJsTools
    {
        /// <summary>
        /// 此类用以描述一个 reveal.js section 的相关属性。
        /// 它表示一个子幻灯片。
        /// 在 reveal.js 中，默认情况下：幻灯片由一个个 <section>...</section> 组成。在一张幻灯片中可以再嵌套一个子层。
        /// 幻灯片水平切换，但子幻灯片垂直切换。
        /// </summary>
        public class RevealJsInnerSlide
        {
            public StringBuilder ContentMarkdownText { get; set; } = new StringBuilder();

            /// <summary>
            /// 当前幻灯片的选项。
            /// </summary>
            protected Dictionary<string, string> slideOptions = new Dictionary<string, string>();

            /// <summary>
            /// 拼接用于幻灯片 &lt;section&gt; 中的 Attribute 字符串。
            /// </summary>
            public string OptionsAttributeText
            {
                get
                {
                    var sb = new StringBuilder();
                    if (slideOptions.ContainsKey("transition"))
                    {
                        var trans = slideOptions["transition"].Split(new char[] { ' ', '　', '\t', }, StringSplitOptions.RemoveEmptyEntries);
                        if (trans.Length > 0)
                        {
                            sb.Append("data-transition=\"");
                            foreach (var tran in trans)
                            {
                                sb.Append(Transition.GetTransitionName(tran));
                                sb.Append(" ");
                            }
                            sb.Append("\"");
                        }
                    }

                    if (slideOptions.ContainsKey("background"))
                    {
                        var bshNames = slideOptions["background"].Split(new char[] { ',', ' ', '，', '　', '\t', '.', '。', }, StringSplitOptions.RemoveEmptyEntries);
                        var bshName = bshNames[bshNames.Length - 1];  // 如果设置了多个色彩，以最后一个为准。

                        var bsh = BrushManager.GetBrush(bshName);
                        if (bsh != null && bsh is SolidColorBrush)
                        {
                            var sbsh = bsh as SolidColorBrush;
                            sb.Append("data-background-color=\"" + $"rgb({sbsh.Color.R},{sbsh.Color.G},{sbsh.Color.B})" + "\" ");
                        }
                        else
                        {
                            sb.Append("data-background-color=\"" + slideOptions["background"] + "\" ");
                        }
                    }

                    if (slideOptions.ContainsKey("backimage"))
                    {
                        sb.Append("data-background-image=\"" + slideOptions["backimage"] + "\" ");
                    }

                    if (slideOptions.ContainsKey("backsize"))
                    {
                        sb.Append("data-background-size=\"" + slideOptions["backsize"] + "\" ");
                    }

                    if (slideOptions.ContainsKey("backposition"))
                    {
                        sb.Append("data-background-position=\"" + slideOptions["backposition"] + "\" ");
                    }

                    if (slideOptions.ContainsKey("backrepeat"))
                    {
                        sb.Append("data-background-repeat=\"" + slideOptions["backrepeat"] + "\" ");
                    }

                    if (slideOptions.ContainsKey("backopacity"))
                    {
                        sb.Append("data-background-opacity=\"" + slideOptions["backopacity"] + "\" ");
                    }

                    return sb.ToString();
                }
            }

            /// <summary>
            /// 写在方块标头的样式表：通常以 $ 开头。在这里用以设置 reveal.js 幻灯片的相关样式属性。
            /// </summary>
            public string HeaderStyleText { get; set; }

            /// <summary>
            /// 写在方块标尾的样式表：通常以 $ 开头。在这里用以设置 reveal.js 幻灯片的相关特效属性。
            /// </summary>
            public string TailStyleText { get; set; }

            /// <summary>
            /// 用于演讲者模式的注释文本（在 Markdown 中应以 ；或 ; 开头）。
            /// </summary>
            public StringBuilder NoteLinesText { get; } = new StringBuilder();

            internal bool ReadSlideOptionLine(string line)
            {
                if (string.IsNullOrWhiteSpace(line)) return false;

                var regTransition = new Regex(@"(?<=(^[;；][ 　\t]*?((transition)|(切换(模式)?))[ 　\t]*?[:：][ 　\t]*?)).*$");
                var matchTransition = regTransition.Match(line);
                if (matchTransition.Success)
                {
                    WriteOption(ref slideOptions, "transition", matchTransition.Value);
                    return true;
                }

                var regBackground = new Regex(@"(?<=(^[;；][ 　\t]*?((background)|(背景(色)?))[ 　\t]*?[:：][ 　\t]*?)).*$");
                var matchBackground = regBackground.Match(line);
                if (matchBackground.Success)
                {
                    WriteOption(ref slideOptions, "background", matchBackground.Value.Trim(new char[] { ' ', '　', '\t', ',', '，', ';', '；', '.', '。', }));
                    return true;
                }

                var regBackImage = new Regex(@"(?<=(^[;；][ 　\t]*?((backimage)|(背景图))[ 　\t]*?[:：][ 　\t]*?)).*$");
                var matchBackImage = regBackImage.Match(line);
                if (matchBackImage.Success)
                {
                    var imagePath = matchBackImage.Value;
                    var regImageLinkPath = new Regex(@"(?<=(!?\[[^\[\]]*?\]\())[^\(\)]*?(?=(\)[ 　\t]{0,}))");
                    var matchImageLinkPath = regImageLinkPath.Match(imagePath);
                    if (matchImageLinkPath.Success)
                    {
                        imagePath = matchImageLinkPath.Value;
                    }

                    WriteOption(ref slideOptions, "backimage", imagePath);
                    return true;
                }

                var regBackSize = new Regex(@"(?<=(^[;；][ 　\t]*?((backsize)|(背景(图)?尺寸))[ 　\t]*?[:：][ 　\t]*?)).*$");
                var matchBackSize = regBackSize.Match(line);
                if (matchBackSize.Success)
                {
                    WriteOption(ref slideOptions, "backsize", matchBackSize.Value);
                    return true;
                }

                var regBackPosition = new Regex(@"(?<=(^[;；][ 　\t]*?((backposition)|(背景(图)?位置))[ 　\t]*?[:：][ 　\t]*?)).*$");
                var matchBackPosition = regBackPosition.Match(line);
                if (matchBackPosition.Success)
                {
                    WriteOption(ref slideOptions, "backposition", matchBackPosition.Value);
                    return true;
                }

                var regBackRepeat = new Regex(@"(?<=(^[;；][ 　\t]*?((backrepeat)|(背景(图)?重复))[ 　\t]*?[:：][ 　\t]*?)).*$");
                var matchBackRepeat = regBackRepeat.Match(line);
                if (matchBackRepeat.Success)
                {
                    WriteOption(ref slideOptions, "backrepeat", matchBackRepeat.Value);
                    return true;
                }

                var regBackOpacity = new Regex(@"(?<=(^[;；][ 　\t]*?((backopacity)|(背景(图)?透明(度)?))[ 　\t]*?[:：][ 　\t]*?)).*$");
                var matchBackOpacity = regBackOpacity.Match(line);
                if (matchBackOpacity.Success)
                {
                    WriteOption(ref slideOptions, "backopacity", matchBackOpacity.Value);
                    return true;
                }

                return false;
            }
        }

    }
}
